package com.knowledge.plus.单例多例.多例;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * springboot 多例
 *
 * @author PF.Tian
 * @since 2021/09/13
 */
@RestController
@Scope("prototype") // 多例
@RequestMapping("request")
class DuoLiController {

    public static long staInvoiceCount = 0;
    public long invoiceCount = 0;

    /**
     * 多例情况:
     * 静态变量被共用，非静态变量不被共用
     */
    @RequestMapping("/test2")
    public String index() {

        return "staInvoiceCount = " + (++staInvoiceCount) + " , invoiceCount= " + (++invoiceCount);
    }
}
/*
    结果：
    第一次请求：staInvoiceCount= 1 , invoiceCount = 1
    第二次请求：staInvoiceCount= 2 , invoiceCount = 1
    第三次请求：staInvoiceCount= 3 , invoiceCount = 1

    结论：多例情况：静态变量被共用，非静态变量不被共用
 */